package com.yeqian.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeqian.controller.CommentController;
import com.yeqian.entity.PageResult;
import com.yeqian.entity.QueryPageBean;
import com.yeqian.pojo.Article;
import com.yeqian.pojo.Comment;
import com.yeqian.service.BaseService;
import com.yeqian.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author : yeqian
 * @Date : 2021/1/7 21:29
 * <p>
 * 评论服务实现类
 */
@Transactional
@Service
public class CommentServiceImpl extends BaseService implements CommentService {

    private Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);

    /**
     * 查询每月的评论数
     *
     * @param month
     * @return java.lang.Integer
     * @author yeqian
     * @date 2021/1/17 12:19
     */
    @Override
    public Integer getCommentCountByMonth(Date month) {
        String date = new SimpleDateFormat("yyyy-MM").format(month) + "-31";
        Integer memberCountAfterDate = commentMapper.getCommentCountByMonth(date);
        return memberCountAfterDate;
    }

    /**
     * 查询所有的评论数
     *
     * @param authorId
     * @return java.lang.Integer
     * @author yeqian
     * @date 2021/1/10 10:28
     */
    @Override
    public Integer getCommentCount(Integer authorId) {
        return commentMapper.getCommentCount(authorId);
    }

    /**
     * 查询未读评论
     *
     * @param author
     * @return java.lang.Integer
     * @author yeqian
     * @date 2021/1/9 11:49
     */
    @Override
    public Integer getUnreadByauthor(Integer author) {
        return commentMapper.getUnreadByauthor(author);
    }

    /**
     * 展示文章对应的评论的回复
     *
     * @param commentId
     * @return java.util.List<com.yeqian.pojo.Comment>
     * @author yeqian
     * @date 2021/1/8 17:19
     */
    @Override
    public List<Comment> getCommentReplyByCommentId(Integer commentId) {
        return commentMapper.getCommentReplyByCommentId(commentId);
    }

    /**
     * 根据文章id查询对应的所有评论
     *
     * @param id
     * @return java.util.List<com.yeqian.pojo.Comment>
     * @author yeqian
     * @date 2021/1/8 14:00
     */
    @Override
    public List<Comment> getCommentByArticleId(Integer id) {
        //查询所有的评论
        List<Comment> commentByArticleId = commentMapper.getCommentByArticleId(id);
        //查询所有评论对应的回复
        for (Comment comment : commentByArticleId) {
            List<Comment> commentReplyByCommentId = getCommentReplyByCommentId(comment.getId());
            // logger.error(comment.getId() + "===>" + commentReplyByCommentId);
            comment.setReply(commentReplyByCommentId);
        }
        return commentByArticleId;
    }

    /**
     * 查询评论
     *
     * @param id
     * @return com.yeqian.pojo.Comment
     * @author yeqian
     * @date 2021/1/7 22:52
     */
    @Override
    public Comment getComment(Integer id) {
        commentMapper.updateCommentStatus(id);
        Comment comment = commentMapper.getComment(id);
        setArticleName(comment);
        return comment;
    }

    /**
     * 评论分页
     *
     * @param queryPageBean
     * @return com.yeqian.entity.PageResult
     * @author yeqian
     * @date 2021/1/7 22:20
     */
    @Override
    public PageResult listPageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage, pageSize);
        Page<Comment> page = commentMapper.getAllConmmentByPage(queryString);
        long total = page.getTotal();
        List<Comment> result = page.getResult();
        for (Comment comment : result) {
            setArticleName(comment);
        }
        return new PageResult(total, result);
    }

    /**
     * 公共代码--设置文章名
     *
     * @param comment
     * @return void
     * @author yeqian
     * @date 2021/1/8 12:41
     */
    public void setArticleName(Comment comment) {
        Integer articleId = comment.getArticleId();
        String articleName = commentMapper.getArticleNameById(articleId);
        comment.setArticleName("《" + articleName + "》");
    }

    /**
     * 删除评论
     *
     * @param conmentId
     * @return void
     * @author yeqian
     * @date 2021/1/7 22:14
     */
    @Override
    public void deleteConment(Integer conmentId) {
        commentMapper.deleteByPrimaryKey(conmentId);
    }

    /**
     * 新增评论
     *
     * @param comment
     * @return void
     * @author yeqian
     * @date 2021/1/7 21:26
     */
    @Override
    public void saveComment(Comment comment) {
        commentMapper.saveComment(comment);
    }
}
